package test.reflect;

import static java.lang.System.out;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;


public class ClassSpy {

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 */
	public static void main(final String[] args) throws ClassNotFoundException {
		String className = "test.reflect.Model";
		Class<?> clazz = Class.forName(className);
		print("Class name", clazz.getName());

		printMembers(clazz.getConstructors(), "Constuctors");
		printMembers(clazz.getFields(), "Fields");
		printMembers(clazz.getMethods(), "Methods");
		printClasses(clazz);

	}

	private static void print(final String message, final String result) {
		System.out.format("%s: %s%n", message, result);
	}

	private static void printMembers(final Member[] mbrs, final String s) {
		out.format("%s:%n", s);
		for (Member mbr : mbrs) {
			if (mbr instanceof Field) {
				out.format("  %s%n", ((Field) mbr).toGenericString());
			} else if (mbr instanceof Constructor) {
				out.format("  %s%n", ((Constructor) mbr).toGenericString());
			} else if (mbr instanceof Method) {
				out.format("  %s%n", ((Method) mbr).toGenericString());
			}
		}
		if (mbrs.length == 0) {
			out.format("  -- No %s --%n", s);
		}
		out.format("%n");
	}

	private static void printClasses(final Class<?> c) {
		out.format("Classes:%n");
		Class<?>[] clss = c.getClasses();
		for (Class<?> cls : clss) {
			out.format("  %s%n", cls.getCanonicalName());
		}
		if (clss.length == 0) {
			out.format("  -- No member interfaces, classes, or enums --%n");
		}
		out.format("%n");
	}


}
